import { authMiddleware } from '@/lib/api';
import { clientQueryPostDetails, queryPath } from '@/services/api';
import { cookies, headers } from 'next/headers';
import Metadata from '@/lib/metadata';
import PostIdPage from '@/app/[locale]/posts/[id]/postid';
import ResetPage from '@/app/[locale]/reset/reset';
import { Metadata as MetadataNext } from 'next';
import type { IPostClientDetails } from '@/interfaces';
import { getMetadata } from '@/lib/tool';
import { notFound } from 'next/navigation';
import PostIdH5Page from '@/app/[locale]/mobile/posts/[id]/h5';
import { createTranslator } from 'next-intl';
import { getMessages } from '@/lib/dictionaries';

export async function generateMetadata({
  params: { id, locale },
  searchParams = {},
}: {
  params: { id?: string; locale: string };
  searchParams: {};
}): Promise<MetadataNext> {
  if (!id) {
    notFound();
  }

  const t = createTranslator(await getMessages(locale));

  try {
    const token = authMiddleware(cookies());
    const response = clientQueryPostDetails({
      baseURL: process.env.APP_API_SERVER,
      token,
      id,
      query: searchParams,
    });
    const resp1 = await ((await response) as Response).json();
    const data = resp1.data as IPostClientDetails;

    return getMetadata({
      title: data.basic.name,
      authors: {
        url: `/${data.basic.createdBy}`,
        name: data.basic.creatorAlias,
      },
      creator: `${data.basic.creatorAlias}(${data.basic.createdBy})`,
      description: data.basic.overview
        ? `${data.basic.overview} - ${data.basic.customTags
            .map((tag: string) => `#${tag}`)
            .join(', ')}` || ''
        : '',
    });
  } catch (e) {
    return getMetadata({ title: t('PostIdPage.articleDetails') });
  }
}

export default async function Page({
  params = {},
  searchParams = {},
}: {
  params: { id?: string };
  searchParams: { v?: 'h5' };
}) {
  if (!params.id) {
    notFound();
  }

  try {
    const token = authMiddleware(cookies());
    const req1 = queryPath({
      baseURL: process.env.APP_API_SERVER,
      token,
      query: {
        name: '/',
      },
    });
    const req2 = clientQueryPostDetails({
      baseURL: process.env.APP_API_SERVER,
      token,
      id: params.id,
      query: searchParams,
    });

    const responses = await Promise.all([req1, req2]);
    const resp1 = await ((await responses[0]) as Response).json();
    const resp2 = await ((await responses[1]) as Response).json();

    const metadata = new Metadata();
    metadata.set('page', ['/paths', '/'], resp1.data);
    metadata.set(
      'postId',
      [
        '/forum',
        '/posts',
        '/client',
        params.id,
        '/details',
        searchParams,
        'infinite',
      ],
      resp2.data,
    );
    metadata.setReferer(headers().get('referer'));
    return searchParams.v === 'h5' ? (
      <PostIdH5Page metadata={metadata.toString()} />
    ) : (
      <PostIdPage metadata={metadata.toString()} />
    );
  } catch (e) {
    return <ResetPage error={e} />;
  }
}
